package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2].toLowerCase();
		
		if (file.exists()) {
			System.out.println("File \"" +file.getAbsolutePath() + "\" already exists, please remove it manually.");
			return;
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(contacts, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		} else {
			System.out.println("Unlnown format: " + format);
			return;
		}
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		FileWriter writer = new FileWriter(file);
		String xml = xstream.toXML(contacts);
		writer.write(xml);
		writer.close();
	}

	public static List<ContactData> loadContactsFromXmlFile(File file) throws IOException {
		XStream xstream = new XStream(); 
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}
	
	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstName()
					+ "," + contact.getLastName() 
					+ "," + contact.getPrimaryAddress() 
					+ "," + contact.getHomePhoneNr() 
					+ "," + contact.getMobilePhoneNr() 
					+ "," + contact.getWorkPhoneNr() 
					+ "," + contact.getFirstEmailAddress() 
					+ "," + contact.getSecondEmailAddress() 
					+ "," + contact.getBirthdayDay() 
					+ "," + contact.getBirthdayMonth() 
					+ "," + contact.getBirthdayYear() 
					+ "," + contact.getContactGroup() 
					+ "," + contact.getSecondaryAddress() 
					+ "," + contact.getSecondaryHomePhoneNr() + ",!" + "\n");
		}
		writer.close();
	}

	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> contacts = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
					.withFirstName(part[0])
					.withLastName(part[1])            
					.withPrimaryAddress(part[2])      
					.withHomePhoneNr(part[3])         
					.withMobilePhoneNr(part[4])       
					.withWorkPhoneNr(part[5])         
					.withFirstEmailAddress(part[6])   
					.withSecondEmailAddress(part[7])  
					.withBirthdayDay(part[8])         
					.withBirthdayMonth(part[9])       
					.withBirthdayYear(part[10])        
					.withContactGroup(part[11])        
					.withSecondaryAddress(part[12])    
					.withSecondaryHomePhoneNr(part[13]);
			contacts.add(contact);
			line = bufferedReader.readLine();
		}
		reader.close();
		return contacts;
	}

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		// get current date here.
		Date date = new Date();
		Locale eng = new Locale("US");
		
		for (int i = 0; i < amount; i++) {
			ContactData contact = new ContactData()
					.withFirstName(generateRandomString())
					.withLastName(generateRandomString())
					.withPrimaryAddress(generateRandomString())
					.withHomePhoneNr(generateRandomNumberString())
					.withMobilePhoneNr(generateRandomNumberString())
					.withWorkPhoneNr(generateRandomNumberString())
					.withFirstEmailAddress(generateRandomString())
					.withSecondEmailAddress(generateRandomString())
					.withBirthdayDay(new SimpleDateFormat("d", eng).format(date))
					.withBirthdayMonth(new SimpleDateFormat("MMMMM", eng).format(date))
					.withBirthdayYear(new SimpleDateFormat("YYYY", eng).format(date))
					.withContactGroup("[none]")
					.withSecondaryAddress(generateRandomString())
					.withSecondaryHomePhoneNr(generateRandomNumberString());
			list.add(contact);
		}
		return list;
	}

  public static String generateRandomString(){
		Random rnd = new Random();
		if (rnd.nextInt(3) == 0){
	    return "";
		} else {
	    return "test" + rnd.nextInt();
		}
  }

  public static String generateRandomNumberString(){
		Random rnd = new Random();
		if (rnd.nextInt(3) == 0){
	    return "";
		} else {
	    return "" + rnd.nextInt();
		}
  }

}
